﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using SyncList.Business.ValueObject;
using SyncList.UserInterface.Helpers;

namespace SyncList.UserInterface
{
	/// <summary>
	/// Interaction logic for UCFileList.xaml
	/// </summary>
	public partial class UCFileList : UserControl
	{

        private bool _readOnly = false;

#region events

        public event EventHandler CloseList;


        private void OnCloseList()
        {
            if (CloseList != null)
                CloseList(this, new EventArgs());
        }

#endregion

        /// <summary>
        /// Constructeur
        /// </summary>
		public UCFileList()
		{
			this.InitializeComponent();
            this.BtnFolderAdd.Click += new RoutedEventHandler(BtnFolderAdd_Click);
            this.BtnFolderRemove.Click += new RoutedEventHandler(BtnFolderRemove_Click);
            this.BtnClose.Click += new RoutedEventHandler(BtnClose_Click);

            this.ImageAdd.Fill = IconHelper.GetImageBrush("add.png");
            this.ImageClose.Fill = IconHelper.GetImageBrush("remove.png");
            this.ImageRemove.Fill = IconHelper.GetImageBrush("remove.png");
		}




        /// <summary>
        /// Données en cours
        /// </summary>
        public Data CurrentData
        {
            get { return ((Data)this.DataContext); }
        }



        /// <summary>
        /// Lecture seule
        /// </summary>
        public bool ReadOnly
        {
            get { return (_readOnly); }
            set
            {
                _readOnly = value;

                System.Windows.Visibility visible = System.Windows.Visibility.Collapsed;
                if (!value)
                    visible = System.Windows.Visibility.Visible;

                this.BtnFolderAdd.Visibility = visible;
                this.BtnFolderRemove.Visibility = visible;
            }
        }


        /// <summary>
        /// Ajouter un dossier
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnFolderAdd_Click(object sender, RoutedEventArgs e)        
        {
            string path = WindowsAccessor.SelectFolder();
            if (!String.IsNullOrWhiteSpace(path))
            {
                Data data = CurrentData;
                if (data != null)
                {                    

                    if(!data.Folders.Contains(path))
                        data.Folders.Add(path);
                }
            }
        }



        /// <summary>
        /// Enlever un dossier
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnFolderRemove_Click(object sender, RoutedEventArgs e)
        {
            string selectedValue = (string)ListBoxFolders.SelectedValue;
            if (!String.IsNullOrWhiteSpace(selectedValue))
            {
                  Data data = CurrentData;
                  if (data != null)
                      data.Folders.Remove(selectedValue);
            }
        }


        /// <summary>
        /// Fermer la liste courante
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            OnCloseList();
        }




        /// <summary>
        /// Mettre à jour la liste des fichiers
        /// </summary>
        public void RefreshFileList()
        {
            BindingExpression expression = this.ListBoxFiles.GetBindingExpression(ListBox.ItemsSourceProperty);
            if(expression != null)
                expression.UpdateTarget();

            BindingExpression expression2 = this.TextBoxFileCount.GetBindingExpression(TextBox.TextProperty);
            if (expression2 != null)
                expression2.UpdateTarget();

        }

    }
}